package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.admin.dto.ChannelDto;
import com.heima.admin.entity.AdChannel;
import com.heima.admin.mapper.AdChannelMapper;
import com.heima.admin.service.IAdChannelService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.common.dto.PageResponseResult;
import com.heima.common.dto.ResponseResult;
import com.heima.media.entity.WmChannel;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 频道信息表 服务实现类
 * </p>
 *
 * @author lixinyu
 * @since 2022-08-04
 */
@Service
public class AdChannelServiceImpl extends ServiceImpl<AdChannelMapper, AdChannel> implements IAdChannelService {

    @Override
    public PageResponseResult findList(ChannelDto channelDto) {
        Page<AdChannel> page = new Page<>(channelDto.getPage(),channelDto.getSize());
        LambdaQueryWrapper<AdChannel> wrapper = new LambdaQueryWrapper<>();
        if(StringUtils.isNotBlank(channelDto.getName())&&!channelDto.getName().equals("")){
            wrapper.eq(AdChannel::getName,channelDto.getName());
        }
        this.page(page,wrapper);
        int size = Integer.parseInt(String.valueOf(page.getSize()));
        return new PageResponseResult(page.getCurrent(),size,page.getTotal(),page.getRecords());
    }

    @Override
    public ResponseResult updateChannel(WmChannel wmChannel) {
        System.out.println("在更新的时候接口获取到的wmChannel："+wmChannel);
        AdChannel adChannel = this.getById(wmChannel.getId());
        System.out.println("获取到的wnChannel的id值是："+wmChannel.getId());
        AdChannel channel = new AdChannel();
        BeanUtils.copyProperties(wmChannel,channel);
        System.out.println("此时要插入到admin数据库中的channel，admin的channel的值是："+channel);
        if(adChannel==null){   //admin数据库没有查询到channel
            this.save(channel);
            return ResponseResult.okResult(channel);
        }
        this.updateById(channel);
        return ResponseResult.okResult(channel);
    }

    @Override
    public ResponseResult deleteChannel(String id) {
        AdChannel adChannel = this.getById(Integer.parseInt(id));
        if(adChannel==null){
            return ResponseResult.okResult();
        }
        this.removeById(Integer.parseInt(id));
        return ResponseResult.okResult();
    }
}
